//
//  CoinVC.m
//  AppNew
//
//  Created by 华伟 on 2019/4/4.
//  Copyright © 2019 华伟. All rights reserved.
//

#import "CoinVC.h"
#import "CoinLeftCell.h"
#import "CoinEachCell.h"
@interface CoinVC ()< UINavigationControllerDelegate,UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>{
    NSInteger _selectIndex;
    UIView *_navView;
}
@property (nonatomic, strong)  UITableView *eachtable;
@property (nonatomic, strong)  UITableView *coinLeftTbl;
@property (nonatomic, strong)  UIScrollView *topScrollView;
@property (nonatomic, strong)  UIView *maskView;
@property (nonatomic, strong)  UIView *btnLineView;
@property (nonatomic, strong)  UILabel *headerTitleLabel;
@property (nonatomic, strong)  NSMutableArray *btnArray;

@end
@implementation CoinVC

- (void)clickedNaviBtn:(UIButton *)btn{ //点击按钮操作
    
    _selectIndex = btn.tag;
    
    CGFloat offSetX = btn.frame.origin.x - _topScrollView.bounds.size.width * 0.5;
    CGFloat offsetX1 = (_topScrollView.contentSize.width - btn.frame.origin.x) - _topScrollView.bounds.size.width * 0.5;
    CGPoint bCenter = CGPointMake(btn.center.x, 42);
    __weak __typeof(self) weakSelf = self;
    
    [_eachtable reloadData];
    [UIView animateWithDuration:0.2 animations:^{
        if (offSetX > 0 && offsetX1 > 0) {
            weakSelf.topScrollView.contentOffset = CGPointMake(offSetX, 0);
        }else if(offSetX < 0){
            weakSelf.topScrollView.contentOffset = CGPointMake(0, 0);
        }else if (offsetX1 < 0){
            weakSelf.topScrollView.contentOffset = CGPointMake(weakSelf.topScrollView.contentSize.width - weakSelf.topScrollView.bounds.size.width , 0);
        }
        weakSelf.btnLineView.center = bCenter;
        NSDictionary *dic = [_coinList objectAtIndex:btn.tag];
        weakSelf.headerTitleLabel.text = [dic objectForKey:@"coinName"];
    }];
    [self requestDataWithIndex:btn.tag];
    [self requsetlist];
    
}

- (void)_initScrollView{ //初始化头部的button scrollview
    
    for (UIView *view in _topScrollView.subviews) {
        [view removeFromSuperview];
    }
    
    _btnArray = @[].mutableCopy;
    for (int i = 0; i < _coinList.count; i++) {
        UIButton *btn = [[UIButton alloc] init];
        btn.frame = CGRectMake(i * 70 , 0, 60, 40);
        btn.tag = i;
        btn.titleLabel.textColor = [UIColor whiteColor];
        NSDictionary *dic = [_coinList objectAtIndex:i];
        [btn addTarget:self action:@selector(clickedNaviBtn:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:[NSString stringWithFormat:@"%@",[dic objectForKey:@"coinName"]] forState:UIControlStateNormal];
        [_topScrollView addSubview:btn];
        [_btnArray addObject:btn];
        
        if ([[dic objectForKey:@"coinName"] isEqualToString:@"RPIC"]) {
            
            _btnLineView = [[UIView alloc] initWithFrame:CGRectMake(btn.center.x - 30, 42, 60, 2)];
            _btnLineView.backgroundColor = [UIColor whiteColor];
            [_topScrollView addSubview:_btnLineView];
            _selectIndex = btn.tag;
            _headerTitleLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"coinName"]];
            
        }
        
    }
    
    if (_btnLineView == nil & _coinList.count == 0) {
        
        _btnLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 42, 60, 2)];
        _btnLineView.backgroundColor = [UIColor whiteColor];
        [_topScrollView addSubview:_btnLineView];
        
        NSDictionary *dic = [_coinList objectAtIndex:0];
        _headerTitleLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"coinName"]];
        
    }

    _topScrollView.contentSize = CGSizeMake(10 * 70, 44);
    
}
- (void)_initNaviView{//初始化头部view
    
    _navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 44+kStatusBarHeight)];
    _navView.backgroundColor = BaseColor;
    [self.view addSubview:_navView];
    
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(5, kStatusBarHeight, 34, 44)];
    leftBtn.imageEdgeInsets = UIEdgeInsetsMake(10, 0, 10, 10);
    [leftBtn setImage:[UIImage imageNamed:@"coinmore"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(showLeft) forControlEvents:UIControlEventTouchUpInside];
    [_navView addSubview:leftBtn];
    
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(35, kStatusBarHeight,ScreenWidth - 50, 44)];
    scroll.showsHorizontalScrollIndicator = false;
    scroll.scrollEnabled = false;
    [_navView addSubview:scroll];
    
    _topScrollView = scroll;
    if (_coinList.count > 0) {
        
        [self requsetlist];
    }
    
}

- (UIView *)_headerView{
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
    v.backgroundColor = [UIColor whiteColor];
    UIView *vleft = [[UIView alloc] initWithFrame:CGRectMake(10, 15, 5, 20)];
    vleft.backgroundColor = BaseColor;
    [v addSubview:vleft];
    _headerTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 100, 30)];
    [v addSubview:_headerTitleLabel];
    UIButton *btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
    btnRight.frame = CGRectMake(ScreenWidth - 40, 10, 30, 30);
    [btnRight setImage:[UIImage imageNamed:@"yan"] forState:UIControlStateNormal];
    [v addSubview:btnRight];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, v.t_bottom - 1, ScreenWidth, 1)];
    lineView.backgroundColor = [UIColor colorFromHexRGB:@"e0e0e0"];
    [v addSubview:lineView];
    return v;
    
}

- (void)_initLeftView{//初始化左边maskview
    
    UITableView *tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, _navView.t_bottom, ScreenWidth, ScreenHeight - NAVBAR_HEIGHT) style:UITableViewStyleGrouped];
    tbl.delegate = self;
    tbl.dataSource = self;
    tbl.tableHeaderView = [self _headerView];
    tbl.tag = 2019;
    [tbl registerNib:[UINib nibWithNibName:@"CoinEachCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CoinEachCellCell"];
        [tbl registerNib:[UINib nibWithNibName:@"CoinDelegateCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CoinDelegate_Cell"];
    tbl.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tbl];
    _eachtable = tbl;
    
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(-ScreenWidth, 0, ScreenWidth, ScreenHeight)];
    [self.view addSubview:leftView];
    
    UIView *leftMain = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, ScreenHeight)];
    leftMain.backgroundColor = [UIColor whiteColor];
    [leftView addSubview:leftMain];
    
    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, NAVBAR_HEIGHT, 100, 30)];
    leftLabel.text = @"币币";
    [leftMain addSubview:leftLabel];
    UIView *tV = [[UIView alloc] initWithFrame:CGRectMake(0, leftLabel.t_bottom+20, leftMain.t_width, 1)];
    tV.backgroundColor = [UIColor colorFromHexRGB:@"e0e0e0"];
    [leftMain addSubview:tV];
    UIImageView *sousuo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sousuo"]];
    sousuo.frame = CGRectMake(10, tV.t_bottom+10, 20, 20);
    [leftMain addSubview:sousuo];
    
    UITextField *textf = [[UITextField alloc] initWithFrame:CGRectMake(sousuo.t_right+5, tV.t_bottom, leftMain.t_width -  sousuo.t_right - 10, 40)];
    textf.borderStyle = UITextBorderStyleNone;
    textf.placeholder = @"搜索币种";
    textf.font = [UIFont systemFontOfSize:15];
    textf.returnKeyType = UIReturnKeySearch;
    textf.delegate = self;
    [leftMain addSubview:textf];
    
    UIView *bV = [[UIView alloc] initWithFrame:CGRectMake(0, textf.t_bottom, leftMain.t_width, 1)];
    bV.backgroundColor = [UIColor colorFromHexRGB:@"e0e0e0"];
    [leftMain addSubview:bV];

    _coinLeftTbl = [[UITableView alloc] initWithFrame:CGRectMake(0, bV.t_bottom, leftMain.t_width, ScreenHeight - bV.t_bottom- NAVBAR_HEIGHT - kTabbarHeight) style:UITableViewStylePlain];
    _coinLeftTbl.delegate = self;
    _coinLeftTbl.dataSource = self;
    _coinLeftTbl.tag = 2020;
    [_coinLeftTbl registerNib:[UINib nibWithNibName:@"CoinLeftCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CoinLeftCellCell"];
    [leftMain addSubview:_coinLeftTbl];
    
    UIView *ritghtV = [[UIView alloc] initWithFrame:CGRectMake(250, 0, ScreenWidth - 250, ScreenHeight)];
    [ritghtV addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideLeft)]];
    [leftView addSubview:ritghtV];
    
    _maskView = leftView;
}

-(void)hideLeft{
    [self.view endEditing:TRUE];
     __weak __typeof(self)weakSelf = self;
    weakSelf.maskView.backgroundColor = [UIColor clearColor];
    [UIView animateWithDuration:0.7 animations:^{
       weakSelf.maskView.frame = CGRectMake(-ScreenWidth, 0, ScreenWidth, ScreenHeight);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isPushSelfClass = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isPushSelfClass animated:animated];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.delegate = self;
    _isBuy = @"1";
    _selectRequestState = @"1";
    _coinList = [NSArray array];
    _dataDic = [NSDictionary dictionary];
    _delegateArr = [NSMutableArray array];
    [self _initNaviView];
    
    [self _initLeftView];

}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self requestDataWithIndex:-1];
    
    
}

- (void)requestDataWithIndex:(NSInteger)index{
    
    NSString *url = @"";
    if (index == -1) {
        url = [NSString stringWithFormat:@"http://119.160.234.147:8080/bbTrade/index"];
    }else if(index != -1 && _coinList.count != 0){
        NSDictionary *dic = [_coinList objectAtIndex:index];
        url = [NSString stringWithFormat:@"http://119.160.234.147:8080/bbTrade/index?b=%@",[dic objectForKey:@"coinName"]];
    }else{
        return;
    }
    [NetRequestTool post:url
                  params:nil
                 success:^(id  _Nonnull json) {
                     
                     self->_dataDic = json;
                     NSDictionary *dic = [json objectForKey:@"data"];
                     BOOL flag = [[json objectForKey:@"flag"] boolValue];
                     if (flag) {
                         
                         NSArray *buyArr = [dic objectForKey:@"buyp"];
                         for (int labelTag = 1 ; labelTag <= buyArr.count ; labelTag ++) {
                             
                             NSDictionary *dataDic = [buyArr objectAtIndex:labelTag - 1];
                             UILabel *saleLabel1 = [self->_detailView viewWithTag:labelTag + 310-1];
                             UILabel *saleLabel2 = [self->_detailView viewWithTag:labelTag + 320-1];
                             
                             NSInteger sellNum = [[dataDic objectForKey:@"amount"] integerValue];
                             float sellPrice = [[dataDic objectForKey:@"price"] floatValue];
                             saleLabel1.text = [NSString stringWithFormat:@"%.3f",sellPrice];
                             saleLabel2.text = [NSString stringWithFormat:@"%ld",sellNum];
                             
                         }
                         
                         
                         NSArray *sellArr = [dic objectForKey:@"buyp"];
                         for (int labelTag = 1 ; labelTag <= sellArr.count ; labelTag ++) {

                             NSDictionary *dataDic = [sellArr objectAtIndex:labelTag - 1];
                             UILabel *saleLabel1 = [self->_detailView viewWithTag:labelTag + 410 -1];
                             UILabel *saleLabel2 = [self->_detailView viewWithTag:labelTag + 420 -1];

                             NSInteger sellNum = [[dataDic objectForKey:@"amount"] integerValue];
                             float sellPrice = [[dataDic objectForKey:@"price"] floatValue];
                             saleLabel1.text = [NSString stringWithFormat:@"%.3f",sellPrice];
                             saleLabel2.text = [NSString stringWithFormat:@"%ld",sellNum];

                             
                         }
                         if (index == -1) {
                             self->_coinList = [dic objectForKey:@"coinList"];
                             
                             [self _initScrollView];
                             
                         }else{
                             
                         }

                         
                     }else{
                         
                         [SVProgressHUD showErrorWithStatus:[json objectForKey:@"msg"]];
                         
                     }
                     
                 } failure:^(NSError * _Nonnull error) {
                     
                 }];
    
//
//    [self requsetlist];

    
}


#pragma mark - actions
- (void)showLeft {
    
    __weak __typeof(self)weakSelf = self;
    [UIView animateWithDuration:0.7 animations:^{
        weakSelf.maskView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    } completion:^(BOOL finished) {
        weakSelf.maskView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
    }];
    
}


#pragma  mark ---lefttableview
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if(tableView.tag == 2019){
        
        if (section == 0) {
            
            UIView  *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 320.f)];
            headView.backgroundColor = [UIColor whiteColor];
            //根据选中的栏目，修改table组头
            _detailView = [self initbuyInView];
            [headView addSubview:_detailView];
            
            return headView;
            
        }else if (section == 1){
            
            UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
            v.backgroundColor = [UIColor whiteColor];
            
            UIView *vleft = [[UIView alloc] initWithFrame:CGRectMake(10, 15, 5, 20)];
            vleft.backgroundColor = BaseColor;
            [v addSubview:vleft];
            
            UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 100, 30)];
            [v addSubview:lbl];
            lbl.text = @"实时成交";
            return v;
            
        }else{
            
            UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
            v.backgroundColor = [UIColor whiteColor];
            UIView *vleft = [[UIView alloc] initWithFrame:CGRectMake(10, 15, 5, 20)];
            vleft.backgroundColor = BaseColor;
            [v addSubview:vleft];
            
            UIButton *currentButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 10, 72, 30)];
            [currentButton setTitle:@"当前成交" forState:UIControlStateNormal];
            [currentButton setTitleColor:BaseColor forState:UIControlStateNormal];
            currentButton.titleLabel.font = [UIFont systemFontOfSize:14];
            currentButton.tag = 3001;
            [currentButton addTarget:self action:@selector(hederAction:) forControlEvents:UIControlEventTouchUpInside];
            [v addSubview:currentButton];
            
            UIButton *oldDelegate = [[UIButton alloc] initWithFrame:CGRectMake(92, 10, 72, 30)];
            [oldDelegate setTitle:@"历史委托" forState:UIControlStateNormal];
            oldDelegate.tag = 3002;
            [oldDelegate setTitleColor:BaseColor forState:UIControlStateNormal];
            oldDelegate.titleLabel.font = [UIFont systemFontOfSize:14];
            [oldDelegate addTarget:self action:@selector(hederAction:) forControlEvents:UIControlEventTouchUpInside];
            [v addSubview:oldDelegate];
            
            UIButton *oldRecord = [[UIButton alloc] initWithFrame:CGRectMake(164, 10, 108, 30)];
            [oldRecord setTitle:@"历史成交记录" forState:UIControlStateNormal];
            oldRecord.tag = 3003;
            [oldRecord setTitleColor:BaseColor forState:UIControlStateNormal];
            oldRecord.titleLabel.font = [UIFont systemFontOfSize:14];
            [oldRecord addTarget:self action:@selector(hederAction:) forControlEvents:UIControlEventTouchUpInside];
            [v addSubview:oldRecord];
            
            if ([_selectRequestState isEqualToString:@"1"]) {
                currentButton.titleLabel.font = [UIFont systemFontOfSize:17];
            }else if ([_selectRequestState isEqualToString:@"2"]) {
                oldDelegate.titleLabel.font = [UIFont systemFontOfSize:17];
            }else if ([_selectRequestState isEqualToString:@"3"]) {
                oldRecord.titleLabel.font = [UIFont systemFontOfSize:17];
            }else{
                
            }
            
            return v;
            
        }
    }else{
        return nil;
    }
    
}

- (void)hederAction:(UIButton *)button{
    
    if (button.tag - 3000 == 1) {
        _selectRequestState = @"1";
    
    }else if (button.tag - 3000 == 2) {
        _selectRequestState = @"2";
    }else if (button.tag - 3000 == 3) {
        _selectRequestState = @"3";
    }
    
    if (_coinList.count > 0) {
        
        [self requsetlist];
    }
    
}

- (void)requsetlist{
    
    NSDictionary *dic = [_coinList objectAtIndex:_selectIndex];
    NSString *url = @"";
    if ([_selectRequestState isEqualToString:@"1"]) {
            url =  [NSString stringWithFormat:@"http://119.160.234.147:8080/bbTrade/getDqwt?coin=%@",[dic objectForKey:@"coinName"]];
    }else if ([_selectRequestState isEqualToString:@"2"]) {
        url =  [NSString stringWithFormat:@"http://119.160.234.147:8080/bbTrade/getLswt?coin=%@",[dic objectForKey:@"coinName"]];
    }else if ([_selectRequestState isEqualToString:@"3"]) {
        url =  [NSString stringWithFormat:@"http://119.160.234.147:8080/bbTrade/getLscj?coin=%@",[dic objectForKey:@"coinName"]];
    }else{
        return;
    }


    [NetRequestTool post:url
                  params:nil
                 success:^(id  _Nonnull json) {
                     
                     BOOL flag = [[json objectForKey:@"flag"] boolValue];
                     if (flag) {
                         
                         self->_delegateArr = [NSMutableArray array];
                         self->_delegateArr = [json objectForKey:@"data"];
                         [self->_eachtable reloadSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
                         
                     }else{
                         
                         [SVProgressHUD showErrorWithStatus:[json objectForKey:@"msg"]];
                         
                     }
                     
                 } failure:^(NSError * _Nonnull error) {
                     
                 }];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(tableView.tag == 2019){
        if (section != 0) {
           
            return 50;
            
        }
        
        return 332.f;
    }else{
        return 0.1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if(tableView.tag == 2019){
        return  3;
    }else{
        return 1;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(tableView.tag == 2019){
        if (section == 0) {
            return 0;
        }else if(section == 1){
            return 10;
        }else {
            return _delegateArr.count;
        }
        
//        return _dataArr.count;
    }else{
        return  30;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(tableView.tag == 2019){
        if (indexPath.section == 0) {
            return 0;
        }else if (indexPath.section == 1){
            return 28.f;
        }else{
            return 134;
        }
        
        //        return _dataArr.count;
    }else{
        return  0;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(tableView.tag == 2019){

        if(indexPath.section == 0){
            
            CoinEachCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CoinEachCellCell" forIndexPath:indexPath];
            if(!cell){
                cell = [[CoinEachCell alloc] init];
            }
            
            if(indexPath.row == 0){
                cell.left.text = @"";
                cell.label1.text = @"价格";
                cell.label2.text = @"数量";
                cell.label3.text = @"累计";
            }else{
                cell.left.text = @"卖6";
                cell.label1.text = @"价格";
                cell.label3.text = @"累计";
                cell.label2.text = [NSString stringWithFormat:@"第%ld%ld个",_selectIndex,indexPath.row];
            }
            return cell;
            
        }else if (indexPath.section == 1){
            
            CoinEachCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CoinEachCellCell" forIndexPath:indexPath];
            if(!cell){
                cell = [[CoinEachCell alloc] init];
            }
            
            if(indexPath.row == 0){
                cell.left.text = @"";
                cell.label1.text = @"时间";
                cell.label2.text = @"方价";
                cell.label3.text = @"数量";
            }else{
                cell.left.text = @"买入";
                cell.label1.text = @"价格";
                cell.label3.text = @"累计";
                cell.label2.text = [NSString stringWithFormat:@"第%ld%ld个",_selectIndex,indexPath.row];
            }
            return cell;
            
        }else{
            
            CoinDelegateCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CoinDelegate_Cell"];
            if (!cell) {
                cell = [[CoinDelegateCell alloc] init];
            }
            NSDictionary *dic = [_delegateArr objectAtIndex:indexPath.row];
            
            
            cell.priceLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"price"]];
            cell.amountLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"all"]];
            cell.totalLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"wqze"]];
            cell.countLabel.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"cjsl"]];
            cell.timeLabel.text = [NSString stringWithFormat:@"时间：%@",[dic objectForKey:@"createDate"]];
            
            cell.timeLabel.text = [NSString stringWithFormat:@"时间：%@",[dic objectForKey:@"createDate"]];
            cell.timeLabel.text = [NSString stringWithFormat:@"时间：%@",[dic objectForKey:@"createDate"]];
            return cell;
            
        }
        
    }else{
        CoinLeftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CoinLeftCellCell" forIndexPath:indexPath];
        if(!cell){
            cell = [[CoinLeftCell alloc] init];
        }
        cell.lbl1.text = [NSString stringWithFormat:@"第%ld个",indexPath.row];
        return cell;
    }
}


#pragma mark --textfielf delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:TRUE];
    return  true;
}


#pragma mark - 交易视图
//买入／卖出
- (UIView *)initbuyInView{
    
    UIView *detailView = [[UIView alloc] initWithFrame:CGRectMake(0 , 12.f, ScreenWidth , 332.f)];
    detailView.backgroundColor = [UIColor whiteColor];
    
    //分成左，右上，右下三部分
    UIView *vLine = [[UIView alloc] initWithFrame:CGRectMake(3 * ScreenWidth / 5 - 0.5 , 8 , 1, detailView.t_height - 12.f)];
    //    vLine.backgroundColor = [UIColor colorFromHexRGB:@"B6B6B6"];
    vLine.backgroundColor = [UIColor clearColor];
    [detailView addSubview:vLine];
    
    UIView *hLine = [[UIView alloc] initWithFrame:CGRectMake(3 * ScreenWidth / 5 + 8 , detailView.t_height / 2 - 0.5 + 8 , 3 * ScreenWidth * 2 / 5 - 16.f , 1)];
    //    hLine.backgroundColor = [UIColor colorFromHexRGB:@"B6B6B6"];
    hLine.backgroundColor = [UIColor clearColor];
    [detailView addSubview:hLine];
    
    //计算右边间隙
    CGFloat tfHeight = (240 - 32) / 5;
    //高度间隙为8
    
    //买入卖出切换按钮
    UIButton *buyInButton = [[UIButton alloc] initWithFrame:CGRectMake(8.f, 8, (3 * ScreenWidth / 5 - 16.f)/2, tfHeight)];
    [buyInButton setBackgroundImage:[UIImage imageNamed:@"btn_tradeIn_off"]
                           forState:UIControlStateNormal];
    [buyInButton setBackgroundImage:[UIImage imageNamed:@"btn_tradeIn_on"]
                           forState:UIControlStateSelected];
    [buyInButton setTitle:@"买入" forState:UIControlStateNormal];
    [buyInButton setTitleColor:[UIColor colorFromHexRGB:@"C7CFCE"]
                      forState:UIControlStateNormal];
    [buyInButton setTitleColor:[UIColor colorFromHexRGB:@"76B7A2"]
                      forState:UIControlStateSelected];
    buyInButton.tag = 600 + 1;
    if ([_isBuy isEqualToString:@"1"]) {
        buyInButton.selected = YES;
    }else{
        buyInButton.selected = NO;
    }
    
    [buyInButton addTarget:self
                    action:@selector(changeStyleAction:)
          forControlEvents:UIControlEventTouchUpInside];
    [detailView addSubview:buyInButton];
    
    UIButton *sellOutButton = [[UIButton alloc] initWithFrame:CGRectMake(buyInButton.t_right,8.f, (3 * ScreenWidth / 5 - 16.f)/2, tfHeight)];
    [sellOutButton setBackgroundImage:[UIImage imageNamed:@"btn_tradeOut_off"]
                             forState:UIControlStateNormal];
    [sellOutButton setBackgroundImage:[UIImage imageNamed:@"btn_tradeOut_on"]
                             forState:UIControlStateSelected];
    [sellOutButton setTitle:@"卖出" forState:UIControlStateNormal];
    [sellOutButton setTitleColor:[UIColor colorFromHexRGB:@"C7CFCE"]
                        forState:UIControlStateNormal];
    [sellOutButton setTitleColor:[UIColor colorFromHexRGB:@"E99D52"]
                        forState:UIControlStateSelected];
    sellOutButton.tag = 600 + 2;
    if ([_isBuy isEqualToString:@"2"]) {
        sellOutButton.selected = YES;
    }else{
        sellOutButton.selected = NO;
    }
    [sellOutButton addTarget:self
                      action:@selector(changeStyleAction:)
            forControlEvents:UIControlEventTouchUpInside];
    [detailView addSubview:sellOutButton];
    
    //价格按钮
    buyInTF = [[UITextField alloc] initWithFrame:CGRectMake(8.f, buyInButton.t_bottom + 16 , 3 * ScreenWidth / 5 - 16.f, tfHeight)];
    buyInTF.tag = 201;
    buyInTF.text = @"0";
    buyInTF.textAlignment = NSTextAlignmentCenter;
    buyInTF.keyboardType = UIKeyboardTypeNumberPad;
    buyInTF.layer.borderWidth = 1;
    buyInTF.leftViewMode = UITextFieldViewModeAlways;
    buyInTF.rightViewMode = UITextFieldViewModeAlways;
    buyInTF.layer.borderColor = [[UIColor colorFromHexRGB:@"B6B6B6"] CGColor];
    //左边右边添加按钮
    UILabel *leftLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(4, 4,tfHeight - 8, tfHeight - 8)];
    leftLabel1.textColor = [UIColor colorFromHexRGB:@"CACFD1"];
    leftLabel1.text = @"价格";
    leftLabel1.numberOfLines = 0;
    leftLabel1.textAlignment = NSTextAlignmentCenter;
    leftLabel1.font = [UIFont systemFontOfSize:14];
    buyInTF.leftView = leftLabel1;
    UILabel *rightLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 44.f, tfHeight - 8)];
    rightLabel1.textColor = [UIColor colorFromHexRGB:@"748688"];
    rightLabel1.text = @"USTD";
    rightLabel1.numberOfLines = 0;
    rightLabel1.textAlignment = NSTextAlignmentCenter;
    rightLabel1.font = [UIFont systemFontOfSize:14];
    buyInTF.rightView = rightLabel1;
    [detailView addSubview:buyInTF];
    
    countTF = [[UITextField alloc] initWithFrame:CGRectMake(8.f, buyInTF.t_bottom + 16 , 3 * ScreenWidth / 5 - 16.f, tfHeight)];
    countTF.tag = 202;
    countTF.textAlignment = NSTextAlignmentCenter;
    //    countTF.keyboardType = UIKeyboardTypeNumberPad;
    countTF.keyboardType = UIKeyboardTypeNumberPad;
    countTF.delegate = self;
    countTF.layer.borderWidth = 1;
    countTF.layer.borderColor = [[UIColor colorFromHexRGB:@"B6B6B6"] CGColor];
    countTF.layer.cornerRadius = 5;
    countTF.layer.masksToBounds = YES;
    countTF.leftViewMode = UITextFieldViewModeAlways;
    countTF.rightViewMode = UITextFieldViewModeAlways;
    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, tfHeight - 8, tfHeight - 8)];
    leftLabel.textColor = [UIColor colorFromHexRGB:@"CACFD1"];
    leftLabel.text = @"数量";
    leftLabel.numberOfLines = 0;
    leftLabel.textAlignment = NSTextAlignmentCenter;
    leftLabel.font = [UIFont systemFontOfSize:14];
    countTF.leftView = leftLabel;
    UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 44, tfHeight - 8)];
    rightLabel.textColor = [UIColor colorFromHexRGB:@"748688"];
    rightLabel.text = @"CSTO";
    rightLabel.numberOfLines = 0;
    rightLabel.textAlignment = NSTextAlignmentCenter;
    rightLabel.font = [UIFont systemFontOfSize:14];
    countTF.rightView = rightLabel;
    
    [detailView addSubview:countTF];
    
    //交易额
    limitMsgLabel = [[UILabel alloc] initWithFrame:CGRectMake(8.f, countTF.t_bottom + 8, 3 * ScreenWidth / 5 - 16.f, tfHeight * 2)];
    limitMsgLabel.tag = 800;
    NSString *attString = @"手续费\n交易额\n-";
    limitMsgLabel.textColor = [UIColor colorFromHexRGB:@"90A09E"];
    limitMsgLabel.text = attString;
    limitMsgLabel.numberOfLines = 0;
    limitMsgLabel.font = [UIFont systemFontOfSize:14];
    [detailView addSubview:limitMsgLabel];
    
    operationButton = [[UIButton alloc] initWithFrame:CGRectMake(8.f, limitMsgLabel.t_bottom + 8 , 3 * ScreenWidth / 5 - 16.f, tfHeight)];
    operationButton.tag = 700;
    operationButton.layer.borderWidth = 1;
    operationButton.layer.borderColor = [[UIColor colorFromHexRGB:@"B6B6B6"] CGColor];
    operationButton.layer.cornerRadius = tfHeight / 2;
    operationButton.layer.masksToBounds = YES;
    [operationButton setTitleColor:[UIColor whiteColor]
                          forState:UIControlStateNormal];
    [operationButton setTitleColor:[UIColor whiteColor]
                          forState: UIControlStateNormal];
    if ([_isBuy isEqualToString:@"1"]) {
        [operationButton setTitle:@"买入"
                         forState: UIControlStateNormal];
        [operationButton setBackgroundColor:[UIColor colorFromHexRGB:@"76B7A2"]];
    }else{
        [operationButton setTitle:@"卖出"
                         forState: UIControlStateNormal];
        [operationButton setBackgroundColor:[UIColor colorFromHexRGB:@"E99D52"]];
    }
    [operationButton addTarget:self
                        action:@selector(operationAction:)
              forControlEvents:UIControlEventTouchUpInside];
    [detailView addSubview:operationButton];
    
    //计算左边间隙和高度
    CGFloat labelWidth = (ScreenWidth * 2 / 5 - 16.f) / 3;
    CGFloat labelHeight = (300 / 2 - 0.5 - 8) / 5;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(3 * ScreenWidth / 5 + 8, 0, labelWidth, labelHeight)];
    label.text = @"盘口";
    label.font = [UIFont systemFontOfSize:12];
    label.textColor = [UIColor colorFromHexRGB:@"B6B6B6"];
    label.textAlignment = NSTextAlignmentLeft;
    [detailView addSubview:label];
    
    UILabel *centerLabel = [[UILabel alloc] initWithFrame:CGRectMake(3 * ScreenWidth / 5 + 8 + labelWidth, 0, labelWidth, labelHeight)];
    centerLabel.text = @"价格";
    centerLabel.font = [UIFont systemFontOfSize:12];
    centerLabel.textColor = [UIColor colorFromHexRGB:@"B6B6B6"];
    centerLabel.textAlignment = NSTextAlignmentRight;
    [detailView addSubview:centerLabel];
    
    UILabel *rLabel = [[UILabel alloc] initWithFrame:CGRectMake(3 * ScreenWidth / 5 + 8 + labelWidth * 2, 0, labelWidth, labelHeight)];
    rLabel.text = @"数量";
    rLabel.font = [UIFont systemFontOfSize:12];
    rLabel.textColor = [UIColor colorFromHexRGB:@"B6B6B6"];
    rLabel.textAlignment = NSTextAlignmentRight;
    [detailView addSubview:rLabel];
    
    //右上
    NSArray *saleArr = @[@"5",@"4",@"3",@"2",@"1"];
    for (int i = 0 ; i < saleArr.count ; i ++) {
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(3 * ScreenWidth / 5 + 8, 16 + (labelHeight) * i, labelWidth, labelHeight)];
        label.tag = 300 + i;
        label.text = [NSString stringWithFormat:@"%@",saleArr[i]];
        label.font = [UIFont systemFontOfSize:13];
        label.textColor = [UIColor colorFromHexRGB:@"B6B6B6"];
        [detailView addSubview:label];
        
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(3 * ScreenWidth / 5 + 8 + labelWidth, 16 + (labelHeight) * i, labelWidth, labelHeight)];
        label1.tag = 310 + i;
        label1.text = [NSString stringWithFormat:@"-"];
        label1.font = [UIFont systemFontOfSize:13];
        label1.textColor = [UIColor colorFromHexRGB:@"DD8967"];
        label1.textAlignment = NSTextAlignmentRight;
        [detailView addSubview:label1];
        
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(3 * ScreenWidth / 5 + 8 + labelWidth * 2, 16 + (labelHeight) * i, labelWidth, labelHeight)];
        label2.tag = 320 + i;
        label2.text = [NSString stringWithFormat:@"-"];
        label2.font = [UIFont systemFontOfSize:13];
        label2.textColor = [UIColor colorFromHexRGB:@"B6B6B6"];
        label2.textAlignment = NSTextAlignmentRight;
        [detailView addSubview:label2];
        
    }
    
    //右下
    NSArray *buyArr = @[@"1",@"2",@"3",@"4",@"5"];
    for (int i = 0 ; i < buyArr.count ; i ++) {
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(3 * ScreenWidth / 5 + 8, hLine.t_bottom + (labelHeight) * i, labelWidth, labelHeight)];
        label.tag = 400 + i;
        label.text = [NSString stringWithFormat:@"%@",buyArr[i]];
        label.font = [UIFont systemFontOfSize:13];
        label.textColor = [UIColor colorFromHexRGB:@"B6B6B6"];
        [detailView addSubview:label];
        
        UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(3 * ScreenWidth / 5 + 8 + labelWidth, hLine.t_bottom + (labelHeight) * i, labelWidth, labelHeight)];
        label1.tag = 410 + i;
        label1.text = [NSString stringWithFormat:@"-"];
        label1.font = [UIFont systemFontOfSize:13];
        label1.textColor = [UIColor colorFromHexRGB:@"65C49B"];
        label1.textAlignment = NSTextAlignmentRight;
        [detailView addSubview:label1];
        
        UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(3 * ScreenWidth / 5 + 8 + labelWidth * 2, hLine.t_bottom + (labelHeight) * i, labelWidth, labelHeight)];
        label2.tag = 420 + i;
        label2.text = [NSString stringWithFormat:@"-"];
        label2.font = [UIFont systemFontOfSize:13];
        label2.textColor = [UIColor colorFromHexRGB:@"B6B6B6"];
        label2.textAlignment = NSTextAlignmentRight;
        [detailView addSubview:label2];
        
    }
//    [self _getData];
    
    return detailView;
    
}

- (void)changeStyleAction:(UIButton *)button{
    
    button.selected = YES;
    if (button.tag == 601) {
        
        _isBuy = @"1";
        UIButton *button2 = [_detailView viewWithTag:602];
        button2.selected = NO;
        UIButton *button3 = [_detailView viewWithTag:700];
        [button3 setTitle:@"买入"
                 forState: UIControlStateNormal];
        [button3 setBackgroundColor:[UIColor colorFromHexRGB:@"76B7A2"]];
        [button2 setNeedsLayout];
        [button2 layoutIfNeeded];
        
    }else if (button.tag == 602) {
        
        _isBuy = @"2";
        UIButton *button2 = [_detailView viewWithTag:601];
        button2.selected = NO;
        UIButton *button3 = [_detailView viewWithTag:700];
        [button3 setTitle:@"卖出"
                 forState: UIControlStateNormal];
        [button3 setBackgroundColor:[UIColor colorFromHexRGB:@"E99D52"]];
        [button2 setNeedsLayout];
        [button2 layoutIfNeeded];
        
    }else{
        
    }
    
}

//买入卖出按钮
- (void)operationAction:(UIButton *)button{
    
    UITextField *codeTF = [_detailView viewWithTag:202];
    if (codeTF.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入数量"];
        return;
    }
    [codeTF resignFirstResponder];
//    [self _commit];
    
}

@end
